<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>机构列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                {if !$isInstitutionAdmin}
                <div class="layui-inline">所属城市</div>
                <div class="layui-inline">
                    <select name="cty_id" lay-verify="">
                        <option value="-1" >全部</option>
                        {volist name="cityList" id="vo"}
                        <option value="{$vo['cty_id']}" >{$vo['cty_name']}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-inline">管理员姓名</div>
                <div class="layui-inline">
                    <input type="text" name="adm_realname" placeholder="请输入姓名" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-inline">机构名称</div>
                <div class="layui-inline">
                    <input type="text" name="ins_name" placeholder="请输入机构名称" autocomplete="off" class="layui-input">
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
                {else/}
                <div class="layui-inline">我的机构</div>
                <div class="layui-inline">
                    <input type="text" name="ins_name" {if $isInstitutionAdmin}value="{$isInstitutionAdmin['ins_name']}"  readonly{/if} placeholder="请输入机构名称" autocomplete="off" class="layui-input">
                </div>
                {/if}
            </div>
        </div>

        <div class="layui-card-body">
            {if !$isInstitutionAdmin}
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
            </div>
            {/if}
            <table id="table" lay-filter="table"></table>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.ins_status == 1){ }}
                <button class="layui-btn layui-btn-xs">已启用</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">已禁用</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
            </script>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('table', function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        table.render({
            elem: '#table'
            , url: "{:url ('admin/Institution/institutionList')}"
            , page: true
            , cols: [[
                {field: 'ins_id', width: 80, title: 'ID', sort: true}
                , {field: 'ins_name', title: '机构名称', align: 'center'}
                , {field: 'ins_mobile', title: '机构电话', align: 'center'}
                , {field: 'ins_location', title: '详细地址', align: 'center'}
                , {field: 'cty_name', title: '所处城市', align: 'center'}
                , {field: 'adm_realname', title: '管理员信息', align: 'center'}
                , {field: 'ins_create_time', title: '加入时间', sort: true, align: 'center'}
                , {field: 'ins_status', title: '状态', templet: '#buttonTpl', align: 'center'}
                , {title: '操作', align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    'cty_id':field.cty_id,
                    "ins_name": field.ins_name,
                    "adm_realname":field.adm_realname,
                }
            });
        });

        //监听工具条
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            var ins_id=data.ins_id;
            if (obj.event === 'del') {
                layer.confirm('确定删除此机构？', function (index) {
                    //获取当前管理员的id
                    var adm_id = data.adm_id;
                    $.ajax({
                        url: "{:url ('admin/admin/adminDelete')}?adm_id=" + adm_id,
                        type: "post",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                });
                            }
                        }
                    })
                    layer.close(index);
                });
            }
            else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '编辑机构'
                    , content: "{:url ('admin/Institution/institutionEdit')}?ins_id="+ins_id
                    , area: ['1200px', '600px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'edit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            $.ajax({
                                url: "{:url ('admin/Institution/institutionEdit')}?ins_id="+ins_id,
                                type: "post",
                                dataType: "json",
                                data:field,
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            table.reload("table"); //数据刷新
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                            // window.location.reload();
                                        });
                                    }
                                }
                            })
                            return false;
                        });
                        submit.trigger('click');
                    }
                    , success: function (layero, index) {

                    }
                })
            }
        });

        //添加职工事件
        var active = {
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加机构'
                    , content: "{:url ('admin/admin/adminAdd')}?adm_role_id="+{$InstitutionRoleId}
                    , area: ['800px', '420px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-admin-add-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //培训结构管理员身份id

                            //field:
                            //adm_mobile: "12312312312"
                            // adm_password: "123"
                            // adm_realname: "123"
                            // adm_role_id: "2"
                            // adm_status: "on"
                            // adm_username: "123"
                            // email: "123@qq.com"
                            // repass: "123"
                            //拿到数据后进行ajax提交数据
                            //发起ajax请求
                            $.ajax({
                                url: "{:url ('admin/admin/adminAdd')}",
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.close(index); //关闭弹层
                                        layer.msg("机构添加成功", {icon: 1, time: 1500}, function () {
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 2500}, function () {
                                        });
                                    }
                                }
                            })
                            return false;
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

